package com.tiennt.android.chinesechess.models;

import java.util.ArrayList;
import java.util.Collections;

import android.util.Log;

import com.tiennt.android.chinesechess.models.quanco.QuanCo;
import com.tiennt.android.chinesechess.models.quanco.QuanMa;
import com.tiennt.android.chinesechess.models.quanco.QuanPhao;
import com.tiennt.android.chinesechess.models.quanco.QuanSy;
import com.tiennt.android.chinesechess.models.quanco.QuanTinh;
import com.tiennt.android.chinesechess.models.quanco.QuanTot;
import com.tiennt.android.chinesechess.models.quanco.QuanTuong;
import com.tiennt.android.chinesechess.models.quanco.QuanXe;

public class Player {

	// Debug purpose.
	private boolean DBG = true;
	private String TAG = "Player";

	public int mPhe;
	public ArrayList<QuanCo> mDSQuanCo = new ArrayList<QuanCo>();

	public ArrayList<Coord> mDSViTriBatDau = new ArrayList<Coord>();

	public QuanTuong mTuong;
	public QuanSy[] mSy = new QuanSy[2];
	public QuanTinh[] mTinh = new QuanTinh[2];
	public QuanXe[] mXe = new QuanXe[2];
	public QuanPhao[] mPhao = new QuanPhao[2];
	public QuanMa[] mMa = new QuanMa[2];
	public QuanTot[] mTot = new QuanTot[5];

	private BanCo mBanCo;

	public Player(int phe, BanCo banCo) {
		mPhe = phe;
		mBanCo = banCo;

		Coord vitri;
		if (mPhe == QuanCo.cPHE_DO) {
			mDSViTriBatDau.add(new Coord(0, 0));
			mDSViTriBatDau.add(new Coord(0, 1));
			mDSViTriBatDau.add(new Coord(0, 2));
			mDSViTriBatDau.add(new Coord(0, 3));
			mDSViTriBatDau.add(new Coord(0, 5));
			mDSViTriBatDau.add(new Coord(0, 6));
			mDSViTriBatDau.add(new Coord(0, 7));
			mDSViTriBatDau.add(new Coord(0, 8));
			mDSViTriBatDau.add(new Coord(2, 1));
			mDSViTriBatDau.add(new Coord(2, 7));
			mDSViTriBatDau.add(new Coord(3, 0));
			mDSViTriBatDau.add(new Coord(3, 2));
			mDSViTriBatDau.add(new Coord(3, 4));
			mDSViTriBatDau.add(new Coord(3, 6));
			mDSViTriBatDau.add(new Coord(3, 8));

			randomViTriKhoiTao();

			vitri = mDSViTriBatDau.get(0);
			mXe[0] = new QuanXe(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mXe[0]);

			vitri = mDSViTriBatDau.get(1);
			mMa[0] = new QuanMa(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mMa[0]);

			vitri = mDSViTriBatDau.get(2);
			mTinh[0] = new QuanTinh(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTinh[0]);

			vitri = mDSViTriBatDau.get(3);
			mSy[0] = new QuanSy(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mSy[0]);

			mTuong = new QuanTuong(mBanCo, 0, 4, QuanCo.cQUANTUONG,
					QuanCo.cPHE_DO, 1, QuanCo.cTRANGTHAI_NORMAL);
			mDSQuanCo.add(mTuong);

			vitri = mDSViTriBatDau.get(4);
			mSy[1] = new QuanSy(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mSy[1]);

			vitri = mDSViTriBatDau.get(5);
			mTinh[1] = new QuanTinh(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTinh[1]);

			vitri = mDSViTriBatDau.get(6);
			mMa[1] = new QuanMa(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mMa[1]);

			vitri = mDSViTriBatDau.get(7);
			mXe[1] = new QuanXe(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mXe[1]);

			vitri = mDSViTriBatDau.get(8);
			mPhao[0] = new QuanPhao(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mPhao[0]);

			vitri = mDSViTriBatDau.get(9);
			mPhao[1] = new QuanPhao(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mPhao[1]);

			vitri = mDSViTriBatDau.get(10);
			mTot[0] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 1,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[0]);

			vitri = mDSViTriBatDau.get(11);
			mTot[1] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 2,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[1]);

			vitri = mDSViTriBatDau.get(12);
			mTot[2] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 3,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[2]);

			vitri = mDSViTriBatDau.get(13);
			mTot[3] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 4,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[3]);

			vitri = mDSViTriBatDau.get(14);
			mTot[4] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DO, 5,
					QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[4]);

		} else {

			mDSViTriBatDau.add(new Coord(9, 0));
			mDSViTriBatDau.add(new Coord(9, 1));
			mDSViTriBatDau.add(new Coord(9, 2));
			mDSViTriBatDau.add(new Coord(9, 3));
			mDSViTriBatDau.add(new Coord(9, 5));
			mDSViTriBatDau.add(new Coord(9, 6));
			mDSViTriBatDau.add(new Coord(9, 7));
			mDSViTriBatDau.add(new Coord(9, 8));
			mDSViTriBatDau.add(new Coord(7, 1));
			mDSViTriBatDau.add(new Coord(7, 7));
			mDSViTriBatDau.add(new Coord(6, 0));
			mDSViTriBatDau.add(new Coord(6, 2));
			mDSViTriBatDau.add(new Coord(6, 4));
			mDSViTriBatDau.add(new Coord(6, 6));
			mDSViTriBatDau.add(new Coord(6, 8));

			randomViTriKhoiTao();

			vitri = mDSViTriBatDau.get(0);
			mXe[0] = new QuanXe(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mXe[0]);

			vitri = mDSViTriBatDau.get(1);
			mMa[0] = new QuanMa(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mMa[0]);

			vitri = mDSViTriBatDau.get(2);
			mTinh[0] = new QuanTinh(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTinh[0]);

			vitri = mDSViTriBatDau.get(3);
			mSy[0] = new QuanSy(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mSy[0]);

			mTuong = new QuanTuong(mBanCo, 9, 4, QuanCo.cQUANTUONG,
					QuanCo.cPHE_DEN, 1, QuanCo.cTRANGTHAI_NORMAL);
			mDSQuanCo.add(mTuong);

			vitri = mDSViTriBatDau.get(4);
			mSy[1] = new QuanSy(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mSy[1]);

			vitri = mDSViTriBatDau.get(5);
			mTinh[1] = new QuanTinh(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTinh[1]);

			vitri = mDSViTriBatDau.get(6);
			mMa[1] = new QuanMa(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mMa[1]);

			vitri = mDSViTriBatDau.get(7);
			mXe[1] = new QuanXe(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mXe[1]);

			vitri = mDSViTriBatDau.get(8);
			mPhao[0] = new QuanPhao(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mPhao[0]);

			vitri = mDSViTriBatDau.get(9);
			mPhao[1] = new QuanPhao(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mPhao[1]);

			vitri = mDSViTriBatDau.get(10);
			mTot[0] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					1, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[0]);

			vitri = mDSViTriBatDau.get(11);
			mTot[1] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					2, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[1]);

			vitri = mDSViTriBatDau.get(12);
			mTot[2] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					3, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[2]);

			vitri = mDSViTriBatDau.get(13);
			mTot[3] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					4, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[3]);

			vitri = mDSViTriBatDau.get(14);
			mTot[4] = new QuanTot(mBanCo, vitri.mHang, vitri.mCot,
					tinhTenKhoiTao(vitri.mHang, vitri.mCot), QuanCo.cPHE_DEN,
					5, QuanCo.cTRANGTHAI_UP);
			mDSQuanCo.add(mTot[4]);
		}

		for (int i = 0; i < mDSQuanCo.size(); i++) {
			QuanCo qc = mDSQuanCo.get(i);
			log(qc.mTen + ": " + qc.mHang + " - " + qc.mCot);
		}

		log("---------------------------------");
	}

	public QuanCo getQuanCoAt(int hang, int cot) {
		if (!mTot[0].isDead() && mTot[0].mHang == hang && mTot[0].mCot == cot) {
			return mTot[0];
		}
		if (!mTot[1].isDead() && mTot[1].mHang == hang && mTot[1].mCot == cot) {
			return mTot[1];
		}
		if (!mTot[2].isDead() && mTot[2].mHang == hang && mTot[2].mCot == cot) {
			return mTot[2];
		}
		if (!mTot[3].isDead() && mTot[3].mHang == hang && mTot[3].mCot == cot) {
			return mTot[3];
		}
		if (!mPhao[0].isDead() && mPhao[0].mHang == hang
				&& mPhao[0].mCot == cot) {
			return mPhao[0];
		}
		if (!mPhao[1].isDead() && mPhao[1].mHang == hang
				&& mPhao[0].mCot == cot) {
			return mPhao[1];
		}
		if (!mXe[0].isDead() && mXe[0].mHang == hang && mXe[0].mCot == cot) {
			return mXe[0];
		}
		if (!mXe[1].isDead() && mXe[1].mHang == hang && mXe[0].mCot == cot) {
			return mXe[1];
		}
		if (!mMa[0].isDead() && mMa[0].mHang == hang && mMa[0].mCot == cot) {
			return mMa[0];
		}
		if (!mMa[1].isDead() && mMa[1].mHang == hang && mMa[0].mCot == cot) {
			return mMa[1];
		}
		if (!mTinh[0].isDead() && mTinh[0].mHang == hang
				&& mTinh[0].mCot == cot) {
			return mTinh[0];
		}
		if (!mTinh[1].isDead() && mTinh[1].mHang == hang
				&& mTinh[0].mCot == cot) {
			return mTinh[1];
		}
		if (!mSy[0].isDead() && mSy[0].mHang == hang && mSy[0].mCot == cot) {
			return mSy[0];
		}
		if (!mSy[1].isDead() && mSy[1].mHang == hang && mSy[0].mCot == cot) {
			return mSy[1];
		}
		if (mTuong.mHang == hang && mTuong.mCot == cot) {
			return mTuong;
		}
		return null;
	}

	private void randomViTriKhoiTao() {
		Collections.shuffle(mDSViTriBatDau);
	}

	private String tinhTenKhoiTao(int hang, int cot) {
		if (mPhe == QuanCo.cPHE_DO) {
			if (hang == 0) {
				if (cot == 0 || cot == 8) {
					return QuanCo.cQUANXE;
				}
				if (cot == 1 || cot == 7) {
					return QuanCo.cQUANMA;
				}
				if (cot == 2 || cot == 6) {
					return QuanCo.cQUANTINH;
				}
				if (cot == 3 || cot == 5) {
					return QuanCo.cQUANSY;
				}
			} else if (hang == 2 && (cot == 1 || cot == 7)) {
				return QuanCo.cQUANPHAO;
			} else if (hang == 3 && (cot % 2 == 0)) {
				return QuanCo.cQUANTOT;
			}
		} else {
			if (hang == 9) {
				if (cot == 0 || cot == 8) {
					return QuanCo.cQUANXE;
				}
				if (cot == 1 || cot == 7) {
					return QuanCo.cQUANMA;
				}
				if (cot == 2 || cot == 6) {
					return QuanCo.cQUANTINH;
				}
				if (cot == 3 || cot == 5) {
					return QuanCo.cQUANSY;
				}
			} else if (hang == 7 && (cot == 1 || cot == 7)) {
				return QuanCo.cQUANPHAO;
			} else if (hang == 6 && (cot % 2 == 0)) {
				return QuanCo.cQUANTOT;
			}
		}
		return null;
	}

	public void setQuanCoDeadByPosition(int hang, int cot) {
		if (mTot[0].mHang == hang && mTot[0].mCot == cot) {
			mTot[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
		}
		if (mTot[1].mHang == hang && mTot[1].mCot == cot) {
			mTot[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
		}
		if (mTot[2].mHang == hang && mTot[2].mCot == cot) {
			mTot[2].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
		}
		if (mTot[3].mHang == hang && mTot[3].mCot == cot) {
			mTot[3].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
		}
		if (mPhao[0].mHang == hang && mPhao[0].mCot == cot) {
			mPhao[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mPhao[1].isDead() && mPhao[1].mHang == hang
				&& mPhao[0].mCot == cot) {
			mPhao[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mXe[0].isDead() && mXe[0].mHang == hang && mXe[0].mCot == cot) {
			mXe[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mXe[1].isDead() && mXe[1].mHang == hang && mXe[0].mCot == cot) {
			mXe[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mMa[0].isDead() && mMa[0].mHang == hang && mMa[0].mCot == cot) {
			mMa[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mMa[1].isDead() && mMa[1].mHang == hang && mMa[0].mCot == cot) {
			mMa[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mTinh[0].isDead() && mTinh[0].mHang == hang
				&& mTinh[0].mCot == cot) {
			mTinh[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mTinh[1].isDead() && mTinh[1].mHang == hang
				&& mTinh[0].mCot == cot) {
			mTinh[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mSy[0].isDead() && mSy[0].mHang == hang && mSy[0].mCot == cot) {
			mSy[0].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (!mSy[1].isDead() && mSy[1].mHang == hang && mSy[0].mCot == cot) {
			mSy[1].mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
		if (mTuong.mHang == hang && mTuong.mCot == cot) {
			mTuong.mTrangThai = QuanCo.cTRANGTHAI_BIAN;
			;
		}
	}

	public void log(final String message) {
		if (DBG) {
			Log.d(TAG, message);
		}
	}
}
